package com.arrayExamples;

/**
 * Created by yijy on 2017/8/18.
 * 最佳利润
 */
public class BestBuyAndSell {

    public int maxProfit(int[] prices) {
        int profit=0;int max=0;
        int low=0,height=0;
        for(int i=prices.length-1;i>0;i--){
            if(prices[i]<prices[i-1]){
                continue;
            }
            for(int j=i-1;j>=0;j--){
                profit = prices[i]-prices[j];

                if(profit>max){
                    low = j;height=i;
                    max=profit;
                }else
                   continue;
            }
            if(i==1){
                break;
            }
        }
        System.out.println(prices[low]+"---------"+prices[height]);
        return max;
    }

    public static void main(String[] args) {
        int[] prices={7, 1, 5, 3, 6, 4};
        int[] prices1={ 1,2};
        int maxProfit=new BestBuyAndSell().maxProfit(prices1);
        System.out.println(maxProfit);
        int maxProfit2=new BestBuyAndSell().maxProfit2(prices);
        System.out.println(maxProfit2);
    }

    /**
     * 方法二
     */
    public int maxProfit2(int[] prices) {
        if(prices.length==0){
            return 0;
        }
        int buy = prices[0];
        int max = 0;
        for(int i=0;i<prices.length;i++){
            if(prices[i]>buy)
                max = Math.max(max,prices[i]-buy);
            else
                buy = prices[i];
        }
        return max;
    }
}
